<?php
/**
 * Created by PhpStorm.
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/10/11 0011
 * Time: 16:18
 */

namespace Admin\Controller;

use Think\Exception;

class CoursewareController extends AdminController
{
    /**课表数据查询
     * @return array
     */
    private function timetable_data($tid = '')
    {
        $head_list = $this->lists('TableHead', ['status' => 1], 'sort asc');
        $list = M('schedule')->field("scid,title,week_time,sort")->order("sort ASC")->select();
        if (!empty($tid)) {
            //查询教师选课时信息
            $courses = M('TeacherCourse')->getFieldById($tid, 'courses');
            $courses_array = explode(',', $courses);
        } else {
            $courses_array = false;
        }
        //数据组合
        $data = array();
        foreach ($list as $v) {
            if (in_array($v['scid'], $courses_array)) {
                $v['val'] = 1;
            }
            $data[$v['sort']][] = $v;
        }

        //数据排序
        foreach ($data as $key => &$val) {
            foreach ($val as $k => $v) {
                $title[$k] = $v['title'];
                $week_time[$k] = $v['week_time'];
                $scid[$k] = $v['scid'];
            }
            array_multisort($week_time, SORT_NUMERIC, SORT_ASC, $val);
        }
        $this->assign('head_list', $head_list);
        $this->assign('_list', $data);
    }

    //排课首页
    public function index($sid = '')
    {

        if (IS_POST) {
            if (empty($sid)) {
                $this->error('请选择学期');
            } else {
                $model = M('TeacherCourse');
                if ($model->where("uid=%d and sid =%d and status=%d", [UID, $sid, 1])->find()) {
                    $this->error('请勿重复提交');
                    exit;
                }
                $courses = I('post.courses');
                $_POST['courses'] = implode(',', $courses);
                $_POST['uid'] = UID;
                $_POST['update_time'] = time();
                if ($model->add($_POST)) {
                    $this->success('操作成功');
                } else {
                    $this->error('操作失败');
                }
            }
        } else {
            $this->timetable_data();
            $seme = $this->lists('Semester', ['status' => 1]);
            $this->assign('seme', $seme);
            $this->meta_title = '教师排课';
            $this->display();
        }

    }

    //课表管理
    public function class_schedule()
    {
        $this->timetable_data();
        $this->meta_title = '课表管理';
        $this->display();
    }

    //新增课表信息
    public function add()
    {
        if (IS_POST) {
            $model = D('Schedule');
            if (!$model->create()) {
                $this->error($model->getError());
            } else {
                $week_time = I('post.week_time');
                foreach ($week_time as $v) {
                    $_POST['week_time'] = $v;
                    $_POST['create_time'] = time();
                    $_POST['update_time'] = time();
                    $result = M('Schedule')->add($_POST);
                }
                if ($result) {
                    $this->success('新增成功', 'class_schedule');
                } else {
                    $this->error('新增失败');
                }
            }
        } else {
            $head_list = $this->lists('TableHead', ['status' => 1], 'sort asc');
            $this->assign('head_list', $head_list);
            $this->meta_title = '新增、编辑课程';
            $this->display();
        }

    }

    //表头设置
    public function headfild()
    {
        $list = $this->lists('TableHead', ['status' => 1], 'sort ASC');
        $this->assign('_list', $list);
        $this->meta_title = '表头设置';
        $this->display();
    }

    //新增表头
    public function addhead($hid = '')
    {
        $model = M('TableHead');
        if (IS_POST) {
            if (I('post.title') && I('post.sort')) {
                if (empty($hid)) {
                    if ($model->add($_POST)) {
                        $this->success('新增成功', 'headfild');
                    } else {
                        $this->error('新增失败');
                    }
                } else {
                    if ($model->where("hid=%d", $hid)->save($_POST)) {
                        $this->success('更新成功', U('courseware/headfild'));
                    } else {
                        $this->error('更新失败');
                    }
                }

            } else {
                $this->error('标题或排序值不能为空');
            }

        } else {
            $info = $model->where("hid=%d", $hid)->find();
            $this->assign('info', $info);
            $this->meta_title = '新增、编辑表头';
            $this->display();
        }
    }

    //删除表头
    public function del($hid = '')
    {
        if (IS_AJAX) {
            $this->delete('TableHead', ['hid' => $hid]);
        }
    }

    //教师课时管理
    public function teacheerhour()
    {
        $prefix = C('DB_PREFIX');
        $l_table = $prefix . 'teacher_course';
        $r_table = $prefix . 'semester';
        $model = M()->table($l_table . ' a')->join($r_table . ' b ON a.sid=b.sid');
        $_list = $this->lists($model, ['a.uid' => UID, 'a.status' => 1], 'a.tid desc', 'a.*,b.title');
        $this->assign('_list', $_list);
        $this->meta_title = '教师课时管理';
        $this->display();
    }

    //查看我的课时
    public function myhour($tid = '')
    {

        $this->timetable_data($tid);
        $this->display();
    }
    //班级排课
    public function class_course()
    {
        if(IS_POST){
            $table_data['gid']=I('post.gid');//年级id
            $table_data['classid']=I('post.classid');//班级id
            $table_data['sid']=I('post.sid');//学期id
            unset($_POST['gid']);
            unset($_POST['classid']);
            unset($_POST['sid']);
            if(empty($_POST)){
                $this->error('排课数据不能为空');
            }else{
                $sdata=putarry($_POST,'subject_name');
                $table_data['uid']=UID;
                $table_data['create_time']=time();
                $table_data['update_time']=time();
                $model=D('ClassTable');
                if(!$model->create()){
                    $this->error($model->getError());
                    exit;
                }
                $model->startTrans();//开启事务
                try{
                    $ctid=$model->add($table_data);//写入主表
                     foreach ($sdata as $v){
                         $v['sid']=$table_data['sid'];
                         $v['ctid']=$ctid;
                         $v['teacher_name']=$teacher_name=M('Member')->where("uid=%d",$v['teacher_id'])->getField('nickname');//老师名
                         $v['subject_name']=$subject_name=$subject_name=M('Subjects')->where("sid=%d",$v['subject_id'])->getField("subject_name");//科目名称
                         M('ClassSublist')->add($v);
                     }
                     $model->commit();
                    $this->success('新增课表成功');
                } catch (Exception $e){
                   $model->rollback();
                    $this->error('新增课表失败');
                }

            }

        }else{
            $s_data=M('Semester')->field("sid,title")->where("status=%d",1)->select();
            $sub_list=M('subjects')->field("sid,subject_name")->where("status=%d",1)->select();
            //查询年级
            $grade_data=M('Grade')->field("gid,class_name")->where("status=%d",1)->select();
            $this->assign('grade_data',$grade_data);
            $this->assign('s_data',$s_data);
            $this->assign('sub_list',$sub_list);
            $this->timetable_data();
            $this->meta_title='新增班级排课';
            $this->display();
        }

    }
    public function classwork(){
        $prefix   = C('DB_PREFIX');
        $a_table=$prefix.'class_table';//排课表
        $b_table=$prefix.'semester';//学期表
        $c_table=$prefix."grade";//年级表
        $d_table=$prefix."class_grade";//班级表
        $model    = M()->table( $a_table.' a' )->join ( $b_table.' b ON a.sid=b.sid' )
            ->join($c_table.' c ON a.gid=c.gid')
            ->join($d_table.' d ON a.classid=d.id');

        $list=$this->lists($model,array('a.status'=>array('egt',0)),"a.ctid desc","a.*,b.title,c.class_name,d.grade_name");
        $this->assign('_list',$list);
        $this->meta_title='班级排课';
        $this->display();
    }

}









